/*
   Copyright 2011 Jason Drake (jadrake75@gmail.com)
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.javad.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import pub.domain.GradientPanel;

@SuppressWarnings("serial")
public class MessagePanel extends GradientPanel {

	private JLabel label = null;
	private String text = null;
	private boolean showIcon = true;
	
	public MessagePanel(String text) {
		this(text,true);
	}
	
	public MessagePanel(String text, boolean showIcon) {
		setDirection(GradientPanel.VERTICAL);
		setForeground(new Color(255, 250, 205));
		setBackground(new Color(255, 255, 224));
		setText(text);
		setIconVisibility(showIcon);
		setPreferredSize(new Dimension(100,75));
		setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		GroupLayout gl_messagePanel = new GroupLayout(this);
		gl_messagePanel.setHorizontalGroup(
			gl_messagePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_messagePanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(getLabel(), GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_messagePanel.setVerticalGroup(
			gl_messagePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_messagePanel.createSequentialGroup()
					.addGap(5)
					.addComponent(getLabel())
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		setLayout(gl_messagePanel);
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	public void setIconVisibility(boolean showIcon) {
		this.showIcon = showIcon;
	}
	
	protected JLabel getLabel() {
		if (label == null) {
			label = new JLabel();
			label.setIconTextGap(10);
			label.setFont(new Font("Tahoma", Font.PLAIN, 10));
			if( showIcon ) {
				label.setIcon(ComponentResources.getIcon("icon.information.medium")); //$NON-NLS-1$
			}
			label.setText(text);
			label.setHorizontalTextPosition(SwingConstants.RIGHT);
		}
		return label;
	}
}
